package service

import (
	"github.com/gin-gonic/gin"
	"perfect-admin/dao"
	"perfect-admin/global"
	"perfect-admin/model"
	"perfect-admin/model/request"
	"perfect-admin/utils"
	"strings"
	"time"
)

func GetOperLogList(queryInfo request.QueryInfo) (err error, total int, logList []model.SysOperLog) {
	table := dao.DB.Model(&model.SysOperLog{}).Select("*")
	if queryInfo.Title != "" {
		table = table.Where(&model.SysOperLog{Title:queryInfo.Title})
	}
	if queryInfo.BusinessType != "" {
		table = table.Where(&model.SysOperLog{BusinessType:queryInfo.BusinessType})
	}
	if queryInfo.OperName != ""{
		table = table.Where(&model.SysOperLog{OperName:queryInfo.OperName})
	}
	if queryInfo.Status != ""{
		table = table.Where(&model.SysOperLog{Status:queryInfo.Status})
	}
	if queryInfo.PageNum != 0 && queryInfo.PageSize != 0 {
		if err = table.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Order("oper_id desc").Find(&logList).Error; err != nil {
			return err, 0, nil
		}
	}else {
		if err = table.Order("oper_id desc").Find(&logList).Error; err != nil {
			return err, 0, nil
		}
	}
	table.Count(&total)
	return
}

// 写入操作日志表
// 该方法后续即将弃用
func AddOneOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string, reqMethod string, latencyTime time.Duration) {
	sysOperLog := model.SysOperLog{}
	sysOperLog.OperIp = clientIP
	sysOperLog.OperLocation = utils.GetLocation(clientIP)
	sysOperLog.Status = utils.IntToString(statusCode)
	sysOperLog.OperName = utils.GetClaims(c).Username
	sysOperLog.RequestMethod = c.Request.Method
	sysOperLog.OperUrl = reqUri
	if reqUri == "/api/v1/login" {
		sysOperLog.BusinessType = "10"
		sysOperLog.Title = "用户登录"
		sysOperLog.OperName = "-"
	} else if strings.Contains(reqUri, "/api/v1/logout") {
		sysOperLog.BusinessType = "11"
	} else if strings.Contains(reqUri, "/api/v1/base/captcha") {
		sysOperLog.BusinessType = "12"
		sysOperLog.Title = "验证码"
	} else {
		if reqMethod == "POST" {
			sysOperLog.BusinessType = "1"
		} else if reqMethod == "PUT" {
			sysOperLog.BusinessType = "2"
		} else if reqMethod == "DELETE" {
			sysOperLog.BusinessType = "3"
		}
	}
	sysOperLog.Method = reqMethod

	api, err := GetApiByPathMethod(reqUri, reqMethod)
	if err != nil {
		global.LOG.Error(reqUri, reqMethod, err)
	}else {
		sysOperLog.Title = api.Description
	}

	b, _ := c.Get("body")
	sysOperLog.OperParam, _ = utils.StructOrMapOrArrayToString(b)
	sysOperLog.CreateBy = utils.GetClaims(c).Username
	sysOperLog.OperTime = time.Now()
	sysOperLog.LatencyTime = (latencyTime).String()
	sysOperLog.UserAgent = c.Request.UserAgent()
	if c.Err() == nil {
		sysOperLog.Status = "0"
	} else {
		sysOperLog.Status = "1"
	}
	if err := dao.DB.Create(&sysOperLog).Error; err != nil{
		global.LOG.Error("创建操作日志失败", err)
	}
}

func BatchDeleteOperLog(ids []int) (err error) {
	err = dao.DB.Unscoped().Model(&model.SysOperLog{}).Where("oper_id in (?)", ids).Delete(&model.SysOperLog{}).Error
	return
}

func GetOperLogByIds(ids []int) (logList []model.SysOperLog, err error) {
	err = dao.DB.Model(&model.SysOperLog{}).Where("oper_id in (?)", ids).Find(&logList).Error
	return
}

func CleanOperLog() error {
	return dao.DB.Unscoped().Delete(&model.SysOperLog{}).Error
}
